import {toast} from 'nc-lightapp-front';
import moment from 'moment';

export default class CardAction {
    constructor(comp) {
        this.comp = comp;
    }

    /**
     * @desc: 手动校验单据
     * @param {String} formCode  表单
     * @return:
     */
    checkBillBymanual = (formCode) => {
        const {form, myLeave} = this.comp.props;
        const {getFormItemsValue} = form;
        let begintime;
        let endtime;
        let length;
        let timeFlag = false;
        let lengthFlag = false;
        let minunit = getFormItemsValue(formCode, 'minunit').value
        if (begintime > endtime) {
            timeFlag = true
        }
        if (!length || !(length - 0)) {
            lengthFlag = true
        }
        if (timeFlag) {
            // 结束时间必须晚于开始时间！
            toast({
                color: 'danger',
                content: myLeave.json['6040-000011']
            })
            return false
        }
        if (timeFlag && lengthFlag) {
            toast({
                color: 'warning',
                content: myLeave.json['6040-000013']
            })
            return false
        }
        return true
    }

    /**
     * @desc: 表单的编辑后事件(变动后触发)  1. 选择类型后，相应的组件修改 2. 选择时间后，动态计算时长
     * @param {Comment} props
     * @param {String} moduleId  区域id
     * @param {String} key  操作的键
     * @param {String} value 当前值
     * @param {String} oldValue 旧值
     * @return:
     */
    onAfterEvent = (props, moduleId, key, value, oldValue) => {
        if (key === 'pk_attend_staff') {
            const {meta, form} = this.comp.props;
            const {setFormItemsValue} = form;
            let temp = meta.getMeta();
            temp[moduleId].items.forEach(item => {
                if (item.attrcode === "fill_type_id") {
                    item.queryCondition = function () {
                        return {
                            pk_attend_staff: value && value.refpk
                        }
                    }
                }
            });
            meta.setMeta(temp);
            setFormItemsValue(moduleId, {
                fill_type_id: {
                    value: '',
                    display: ''
                }
            })
        }
    }

    syncReason = () => {
        const {editTable, form} = this.comp.props;
        let reason = form.getFormItemsValue("attendance_card", "fill_reason").value || "";
        let allTableData = editTable.getAllRows('batch_attend_edit_list') || [];
        allTableData.map((row, index) => {
                editTable.setValByKeyAndIndex('batch_attend_edit_list', index, 'fill_reason', {
                    value: reason
                });
            }
        )
    }

    /**
     * @desc: 计算出可以填补单据的日期
     * @param {type}
     * @return: [Boolean]
     */
    isIncludeLimitDate = (limit, curDate) => {
        const {myLeave} = this.comp.props;
        limit = limit - 0
        if (limit === -1) {
            return false
        }
        let expectedDate = moment().subtract(limit, 'days').startOf('day').format('YYYY-MM-DD HH:mm:ss');
        if (curDate < expectedDate) {
            toast({
                color: 'warning',
                content: myLeave.json['6040-000036']
            })
            return true
        }
    }

    /**
     * @desc: 请假时间补充 时 分 秒  半天或者天假别 都会用到begintime、endtime 字段去计算时长，需要把时间copy到begintime字段上
     *        上午 统一添加 08:00:00 下午 统一添加 20:00:00
     *        整天的统一为 00:00:00  date类型控件选到的为当前时间精确到秒~~~
     * @param  value {String} 当前时间
     * @param  type {Number} 替换的类型 0 '00:00:00' 1 '08:00:00' 2 '20:00:00' 3 '23:59:59'
     * @return: 替换后的时间
     */
    fillDateTime = (value, type) => {
        const timeArr = ['00:00:00', '08:00:00', '20:00:00', '23:59:59'];
        let arr = value.split(' ');
        arr.splice(1, 1, timeArr[type])
        return arr.join(' ')
    }

    onBeforeEvent = () => {
        return true
    }

    addOneRow = () => {
        const {editTable, form} = this.comp.props;
        let reason = form.getFormItemsValue('attendance_card', 'fill_reason').value || '';
        editTable.addRow('batch_attend_edit_list', undefined, true, {
            fill_reason: {
                value: reason
            }
        });
    }

    removeRows = () => {
        const {editTable, myLeave} = this.comp.props;
        let selectData = editTable.getCheckedRows('batch_attend_edit_list') || [];
        let deleteDataIndex = [];
        if (selectData.length) {
            selectData.forEach((v, k) => {
                deleteDataIndex.push(v.index)
            })
            editTable.deleteTableRowsByIndex('batch_attend_edit_list', deleteDataIndex, true);
        } else {
            toast({
                color: 'warning',
                content: myLeave.json['hrkq-0000135'] // 请选择要删除的管理对象！
            });
        }
    }
}
